﻿package com.newbee.activity.tab;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.hhu.newbee.R;
import com.newbee.activity.VoiceSpeachActivity;
import com.newbee.app.OnRefrushActivity;
import com.newbee.app.SpeechApp;
import com.newbee.service.MainService;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.readystatesoftware.viewbadger.BadgeView;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnRefrushActivity,
		android.widget.CompoundButton.OnCheckedChangeListener
{
	/** Called when the activity is first created. */
	private TabHost tabHost;

	private BadgeView mMsgTip;

	public static final String SP_USR_LOGIN_STATUS = "SP_USR_LOGIN_STATUS";

	public static final String LOGIN_STATUE = "LOGIN_STATUE";

	RadioButton mMsgButton;

	private enum CurrentTab
	{

		HOME, FRIEND, QUNZU, MSG

	};

	private Drawable img_b, img_f, img_b_red;

	private CurrentTab mNowTab;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Log.d("wowotou", "-----------------------------------> ");
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		SpeechApp.add(this);
		tabHost = this.getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, VoiceSpeachActivity.class);
		spec = tabHost.newTabSpec("主页").setIndicator("主页").setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, NewFriendsActivity.class);
		spec = tabHost.newTabSpec("朋友").setIndicator("朋友").setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, NewQunZu.class);
		spec = tabHost.newTabSpec("群组").setIndicator("群组").setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, NewMsgActivity.class);
		spec = tabHost.newTabSpec("消息").setIndicator("消息").setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
		mNowTab = CurrentTab.HOME;

		mMsgButton = (RadioButton) this.findViewById(R.id.main_tab_msg);

		// mMsgTip = new BadgeView(this, mMsgButton);
		//
		// mMsgTip.setText("!");
		//
		// mMsgTip.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
		// mMsgTip.show();

		Resources res = getResources();
		img_f = res.getDrawable(R.drawable.main_msg_f);
		// 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
		img_f.setBounds(0, 0, img_f.getMinimumWidth(), img_f.getMinimumHeight());
		img_b = res.getDrawable(R.drawable.main_msg_b);
		// 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
		img_b.setBounds(0, 0, img_b.getMinimumWidth(), img_b.getMinimumHeight());
		img_b_red = res.getDrawable(R.drawable.main_msg_b_red);
		// 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
		img_b_red.setBounds(0, 0, img_b_red.getMinimumWidth(),
				img_b_red.getMinimumHeight());

		mMsgButton.setOnCheckedChangeListener(this);

		MainService.speticalAcitivity.put(this.getClass().getName(), this);

		RadioGroup radioGroup = (RadioGroup) this
				.findViewById(R.id.main_tab_group);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				// TODO Auto-generated method stub
				switch (checkedId)
				{
				case R.id.main_tab_shouye:// 添加考试
					tabHost.setCurrentTabByTag("主页");
					mNowTab = CurrentTab.HOME;
					break;
				case R.id.main_tab_pengyou:// 我的考试
					tabHost.setCurrentTabByTag("朋友");
					mNowTab = CurrentTab.FRIEND;
					break;
				case R.id.main_tab_qunzhu:// 我的通知
					tabHost.setCurrentTabByTag("群组");
					mNowTab = CurrentTab.QUNZU;
					break;
				case R.id.main_tab_msg:// 我的通知
					tabHost.setCurrentTabByTag("消息");
					mNowTab = CurrentTab.MSG;
					// this.mMsgButton.setBackgroundResource(R.drawable.bg_checkbox_menus_3);
					break;
				default:
					break;
				}
			}
		});
	}

	@Override
	public void init()
	{

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{

		switch (taskID)
		{
		case TaskType.MAIN_MSG:
		{
			if (mNowTab != CurrentTab.MSG)
			{
				mMsgButton.setCompoundDrawables(null, img_b_red, null, null); // 设置左图标
			}
			break;
		}
		default:
			break;
		}

		return null;
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();

		SharedPreferences sp = this.getSharedPreferences(SP_USR_LOGIN_STATUS,
				Activity.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putBoolean(LOGIN_STATUE, false);
		editor.apply();
		SmackClient.getInstance().logout();
		MainService.setStop();

	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1)
	{

		if (arg1)
		{
			mMsgButton.setCompoundDrawables(null, img_f, null, null); // 设置左图标
		}
		else
		{
			mMsgButton.setCompoundDrawables(null, img_b, null, null); // 设置左图标
		}

	}
}